<template>
  <el-drawer
    v-model="showDetail"
    title="视频详细"
    @close="handleDetailClose"
    custom-class="drawer-class"
  >
    <!-- 图片 -->
    <div class="item">
      <div class="body">
        <div class="video-box">
          <video
            ref="videoPlayer"
            class="w-full w-full"
            :src="detail.videoUrl"
            :poster="detail.thumbnailUrl"
            :autoplay="false"
            controls
          ></video>
        </div>
      </div>
    </div>
    <!-- 时间 -->
    <div class="item">
      <div class="tip">时间</div>
      <div class="body">
        <div>提交时间：{{ formatTime(detail.createTime, 'yyyy-MM-dd HH:mm:ss') }}</div>
        <div>生成时间：{{ formatTime(detail.finishTime, 'yyyy-MM-dd HH:mm:ss') }}</div>
      </div>
    </div>

    <!-- 地址 -->
    <div class="item">
      <div class="tip">视频地址</div>
      <div class="body">
        {{ detail.videoUrl }}
      </div>
    </div>

    <div v-if="detail.imageUrl" class="item">
      <div class="tip">视频垫图</div>
      <div class="body">
        <el-image :src="item"
                  v-for="(item, index) in detail.imageUrl.split(',')" :key="index"/>
      </div>
    </div>
  </el-drawer>
</template>

<script setup lang="ts">
import { VideoApi, VideoVO, } from '@/api/ai/video'
import { formatTime } from '@/utils'

const showDetail = ref<boolean>(false) // 是否显示
const detail = ref<VideoVO>({} as VideoVO) // 图片详细信息

const props = defineProps({
  show: {
    type: Boolean,
    require: true,
    default: false
  },
  id: {
    type: Number,
    required: true
  }
})

/** 关闭抽屉  */
const handleDetailClose = async () => {
  emits('handleDetailClose')
}

/** 监听 drawer 是否打开 */
const { show } = toRefs(props)
watch(show, async (newValue, oldValue) => {
  showDetail.value = newValue as boolean
})

/**  获取图片详情  */
const getVideoDetail = async (id: number) => {
  detail.value = await VideoApi.getVideo(id)
}

/** 监听 id 变化，加载最新图片详情 */
const { id } = toRefs(props)
watch(id, async (newVal, oldVal) => {
  if (newVal) {
    await getVideoDetail(newVal)
  }
})

const emits = defineEmits(['handleDetailClose'])
</script>
<style scoped lang="scss">
.image{
  padding: 5px;
}
.item {
  margin-bottom: 20px;
  width: 100%;
  overflow: hidden;
  word-wrap: break-word;

  .header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .tip {
    font-weight: bold;
    font-size: 16px;
  }

  .body {
    margin-top: 10px;
    color: #616161;

    .taskDetail {
      border-radius: 10px;
    }
  }
}
</style>
